package com.exam.util;

import java.math.BigDecimal;
import java.util.Random;
import java.util.UUID;

public class StringUtils {
	//判断字符串是否为空 为空返回true
	public static boolean isEmpty(String s) {
		if (s == null) {
			return true;
		}
		int length = s.length();
		if (length == 0) {
			return true;
		}
		//检查每个字符串是否为空白字符
		//Character.isWhitespace() 方法,判断字符是否为空白字符 是返回true
		//s.charAt(i)获取每个字符串元素
		for (int i = 0; i < length; i++) {
			if (!Character.isWhitespace(s.charAt(i))) {
				return false; //不为空
			}
		}
		return true;
	}

	public static boolean isNotEmpty(String s) {
		return !isEmpty(s);
	}

	public static Long parseLong(String s) {
		if (isEmpty(s)) {
			return null;
		}
		Long l = null;
		try {
			l = Long.parseLong(s);
		} catch (Exception e) {
		}
		return l;
	}

	public static Integer parseInteger(String s) {
		if (isEmpty(s)) {
			return null;
		}
		Integer i = null;
		try {
			i = Integer.parseInt(s);
		} catch (Exception e) {
		}
		return i;
	}

	public static Double parseDouble(String s) {
		if (isEmpty(s)) {
			return null;
		}
		Double d = null;
		try {
			d = Double.parseDouble(s);
		} catch (Exception e) {
		}
		return d;
	}

	public static BigDecimal parseBigDecimal(String s) {
		if (isEmpty(s)) {
			return null;
		}
		try {
			return new BigDecimal(s);
		} catch (Exception e) {
		}
		return null;
	}

	public static String toString(Object o) {
		if (o == null) {
			return null;
		}
		return o.toString();
	}

	public static String random(int len) {
		String uuid = UUID.randomUUID().toString();
		int length = uuid.length();
		if (len >= length) {
			len = 6;
		}
		Random r = new Random();
		int startIndex = r.nextInt(length - len);
		return uuid.substring(startIndex, startIndex + len);
	}

}
